package com.qding.park.manager.web.controller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.qding.park.common.core.annotation.log.OperationLogType;
import com.qding.park.common.core.annotation.log.SystemControllerLog;
import com.qding.park.common.core.exceptions.ExceptionMessage;
import com.qding.park.common.core.exceptions.auth.AuthException;
import com.qding.park.common.core.validate.RchlValidateBean;
import com.qding.park.common.core.validate.RchlValidateTools;
import com.qding.park.common.utils.DateUtils;
import com.qding.park.common.utils.ListUtil;
import com.qding.park.common.web.controller.BaseController;
import com.qding.park.manager.common.pojo.ParkDataResult;
import com.qding.park.manager.common.rest.domain.AnswerMsg;
import com.qding.park.manager.common.rest.domain.AnswerMsgFactoryImpl;
import com.qding.park.manager.facade.core.service.common.ParkCommonFacade;
import com.qding.park.manager.facade.core.service.entity.ParkStatu;
import com.qding.park.manager.facade.statis.service.ParkStatisFacade;
import com.qding.park.manager.pojo.PowerInfo;
import com.qding.park.manager.web.power.Power;

/**
  * @ClassName: ParkController
  * @Description: TODO
  * @author Comsys-Administrator
  * @date 2016年9月24日 上午10:16:07
  *
  */
@RestController
@EnableAutoConfiguration
@RequestMapping("/park")
public class Web_ParkController extends BaseController{
    private static final Logger _log = LoggerFactory.getLogger(Web_ParkController.class);
	
	@Autowired
  	private  ParkCommonFacade  parkCommonFacade;
	
	@Autowired
	private ParkStatisFacade  parkStatisFacade;
	
	
	/**
	  * overview(车场综缆)
	  *
	  * @Title: overview
	  * @Description: TODO
	  * @param @return    设定文件
	  * @return AnswerMsg    返回类型
	  * @throws
	  */
	
	@SystemControllerLog(description="车场综览",operation=OperationLogType.park_overview)
	@RequestMapping("/overview")
	public AnswerMsg overview(String jumpPage,String pageLength) {
			/******验证字段********/
			RchlValidateBean vb = new RchlValidateBean();
			vb.getRequiredValidate().put("jumpPage", jumpPage);
			vb.getRequiredValidate().put("pageLength", pageLength);
			
			vb.getIntValidate().put("jumpPage", jumpPage);
			vb.getIntValidate().put("pageLength", pageLength);
			RchlValidateTools.validate(vb);
			/**************/
			
			PowerInfo power=	this.getPowerInfo();
			
			int jumpPageInt=Integer.valueOf(jumpPage);
			int pageLengthInt=Integer.valueOf(pageLength);
			ParkDataResult dataResult =parkStatisFacade.overview(jumpPageInt, pageLengthInt,power);
			AnswerMsg answerMsg = AnswerMsgFactoryImpl.createAnswerMsg(dataResult.getData());
			return answerMsg;
	}
	
	
	@Power(visitPark = "#parkId")
	@SystemControllerLog(description="车场状态",operation=OperationLogType.park_status)
	@RequestMapping("/status")
	public AnswerMsg status(String parkId) {
			/******验证字段********/
			RchlValidateBean vb = new RchlValidateBean();
			vb.getRequiredValidate().put("parkId", parkId);
			
			vb.getIntValidate().put("parkId", parkId);
			RchlValidateTools.validate(vb);
			/**************/
			
	        ParkStatu parkStatu= parkCommonFacade.getParkStatus( Integer.valueOf(parkId));
	        
	        ParkDataResult parkDataResult = ParkDataResult.createObjDate();
	        parkDataResult.putDataOnObj("remainParkingSpaces",parkStatu.getTempStopNum() );
	        parkDataResult.putDataOnObj("onlineStatus",DateUtils.formatDate(parkStatu.getOnLineTime(), DateUtils.DATE_FORMAT_DATETIME)+" "+parkStatu.getRemark() );
			AnswerMsg answerMsg = AnswerMsgFactoryImpl.createAnswerMsg(parkDataResult.getData());
			return answerMsg;
	}
	
	@SystemControllerLog(description="车场列表查询",operation=OperationLogType.park_listall)
	@RequestMapping("/listall")
	public AnswerMsg listall() {
			PowerInfo power=	this.getPowerInfo();
			
			if(!power.isAdminIs()&&ListUtil.isEmpty(power.getParkList())){
				throw new AuthException(AuthException.TypeEnum.EXCEPTION_AUTH.getId(), null,
						ExceptionMessage.createExceptionMessage(
								AuthException.TypeEnum.EXCEPTION_AUTH.getDesc(), this.getUsername()+":权限不足!", "权限不足:"));
			}
			
			ParkDataResult dataResult =parkStatisFacade.listall(power);
			AnswerMsg answerMsg = AnswerMsgFactoryImpl.createAnswerMsg(dataResult.getData());
			return answerMsg;
	}
	
//	@SystemControllerLog(description="车场列表查询",operation=OperationLogType.park_listall)
	@RequestMapping("/listTree")
	public AnswerMsg listTree() {
			ParkDataResult dataResult =parkStatisFacade.listTree();
			AnswerMsg answerMsg = AnswerMsgFactoryImpl.createAnswerMsg(dataResult.getData());
			return answerMsg;
	}
}
